package com.googlecode.protoserv.samples;

import com.googlecode.protoserv.Protocol.RpcStatus;
import com.googlecode.protoserv.Rpc;
import com.googlecode.protoserv.RpcCallback;

public class CalcClient implements RpcCallback<CalcResponse> {

  private CalcServiceStub calcService = new CalcServiceStub(
      "http://127.0.0.1:8080/");

  public void NoCallback() {
    Rpc<CalcResponse> rpc = calcService.add(null, 2.0, 2.0);
    if (rpc.waitForResponse() == RpcStatus.SUCCESS) {
      System.out.println(rpc.getResponse().getValue());
    }
  }

  public void NoCallbackWithDeadline() {
    Rpc<CalcResponse> rpc = new Rpc<CalcResponse>();
    rpc.setDeadlineMs(100);
    calcService.add(rpc, 2.0, 2.0);
    if (rpc.waitForResponse() == RpcStatus.SUCCESS) {
      System.out.println(rpc.getResponse().getValue());
    }
  }

  public void RpcDone(Rpc<CalcResponse> rpc) {
    if (rpc.getStatus() == RpcStatus.SUCCESS) {
      System.out.println(rpc.getResponse().getValue());
    }
  }

  public void Callback() {
    calcService.add(new Rpc<CalcResponse>(this), 2.0, 2.0);
  }

  public void CallbackWithDeadline() {
    Rpc<CalcResponse> rpc = new Rpc<CalcResponse>(this);
    rpc.setDeadlineMs(100);
    calcService.add(rpc, 2.0, 2.0);
  }

  public static void main(String[] args) {
    CalcClient client = new CalcClient();
    client.NoCallback();
    client.NoCallbackWithDeadline();
    client.Callback();
    client.CallbackWithDeadline();
  }
}
